package com.genius.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.genius.annotation.Log;
import com.genius.annotation.PreFuncAuth;
import com.genius.core.JSONResult;
import com.genius.enums.BusinessType;
import com.genius.service.NoticeService;
import com.genius.service.NoticeUserService;
import com.genius.system.bo.*;
import com.genius.system.dto.NoticeUserDTO;
import com.genius.system.pojo.Notice;
import com.genius.system.pojo.NoticeUser;
import com.genius.utils.PageUtils;
import com.genius.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
@Api(value = "消息管理接口", tags = {"用于消息管理的接口"})
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private NoticeUserService noticeUserService;

    @PostMapping("/selectNoticeById")
    @ApiOperation(value = "查询消息（根据ID查询）",notes = "根据ID查询消息", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:SELECT")
    public JSONResult selectNoticeById(@Valid @RequestBody SelectNoticeByIdBO selectNoticeByIdBO) {
        Notice notice = noticeService.selectNoticeById(selectNoticeByIdBO.getNoticeId());
        Map<String, Object> result = new HashMap<>();
        result.put("notice",notice);
        return JSONResult.ok(result);
    }

    @PostMapping("/selectNoticeByPage")
    @ApiOperation(value = "查询消息（分页查询）",notes = "查询消息分页数据", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:SELECT")
    public JSONResult selectNoticeByPage(@Valid @RequestBody SelectNoticeByPageBO selectNoticeByIdBO) {
        int pageNum = selectNoticeByIdBO.getPageNum();
        int pageSize = selectNoticeByIdBO.getPageSize();
        Notice notice = JSONUtil.parse(selectNoticeByIdBO).toBean(Notice.class);
        PageUtils.startPage(pageNum,pageSize);
        List<Notice> noticeList = noticeService.selectNoticeByPage(notice);
        Map<String,Object> result = PageUtils.getResultTableData(noticeList);
        return JSONResult.ok(result);
    }

    @PostMapping("/insertNotice")
    @ApiOperation(value = "添加消息",notes = "添加消息", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:INSERT")
    public JSONResult insertNotice(@Valid @RequestBody InsertNoticeBO insertNoticeBO) {
        Notice notice = JSONUtil.parse(insertNoticeBO).toBean(Notice.class);
        Long noticeId = IdUtil.getSnowflake().nextId();
        notice.setNoticeId(noticeId);
        notice.setCreateBy(SecurityUtils.getUsername());
        int rows = noticeService.insertNotice(notice);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/updateNotice")
    @ApiOperation(value = "修改消息",notes = "修改消息", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:UPDATE")
    public JSONResult updateNotice(@Valid @RequestBody UpdateNoticeBO updateNoticeBO) {
        Notice notice = JSONUtil.parse(updateNoticeBO).toBean(Notice.class);
        notice.setUpdateBy(SecurityUtils.getUsername());
        int rows = noticeService.updateNotice(notice);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteNoticeByIds")
    @ApiOperation(value = "删除消息",notes = "删除消息", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:DELETE")
    @Log(title = "删除消息", businessType = BusinessType.DELETE)
    public JSONResult deleteNoticeByIds(@Valid @RequestBody DeleteNoticeByIdsBO deleteNoticeByIdsBO) {
        int rows = noticeService.deleteNoticeByIds(deleteNoticeByIdsBO.getNoticeIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/selectNoticeUserByPage")
    @ApiOperation(value = "查询消息用户（分页查询）",notes = "查询消息用户分页数据", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:SELECT")
    public JSONResult selectNoticeUserByPage(@Valid @RequestBody SelectNoticeUserByPageBO selectNoticeUserByIdBO) {
        int pageNum = selectNoticeUserByIdBO.getPageNum();
        int pageSize = selectNoticeUserByIdBO.getPageSize();
        NoticeUser noticeUser = JSONUtil.parse(selectNoticeUserByIdBO).toBean(NoticeUser.class);
        PageUtils.startPage(pageNum,pageSize);
        List<NoticeUserDTO> noticeUserDTOList = noticeUserService.selectNoticeUserByPage(noticeUser);
        Map<String,Object> result = PageUtils.getResultTableData(noticeUserDTOList);
        return JSONResult.ok(result);
    }

    @PostMapping("/sendNotice")
    @ApiOperation(value = "发送消息",notes = "发送消息", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:SEND")
    public JSONResult sendNotice(@Valid @RequestBody InsertNoticeUserBO insertNoticeUserBO) {
        Long noticeId = insertNoticeUserBO.getNoticeId();
        Long[] userIds = insertNoticeUserBO.getUserIds();
        int rows = noticeUserService.insertNoticeUser(noticeId,userIds);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/revokeNotice")
    @ApiOperation(value = "撤回消息",notes = "撤回消息", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:REVOKE")
    public JSONResult revokeNotice(@Valid @RequestBody RevokeNoticeBO revokeNoticeBO) {
        Long[] noticeUserIds = revokeNoticeBO.getNoticeUserIds();
        int rows = noticeUserService.revokeNoticeUserByIds(noticeUserIds);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteNoticeUserByIds")
    @ApiOperation(value = "删除消息用户",notes = "删除消息用户", httpMethod = "POST")
    @PreFuncAuth("SYS:NOTICE:DELETE")
    public JSONResult deleteNoticeUserByIds(@Valid @RequestBody DeleteNoticeUserByIdsBO deleteNoticeUserByIdsBO) {
        Long[] noticeUserIds = deleteNoticeUserByIdsBO.getNoticeUserIds();
        int rows = noticeUserService.deleteNoticeUserByIds(noticeUserIds);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
